package com.moli.iov.admin.service.impl;

import com.moli.iov.admin.model.vo.res.VehicleTboxDetailVo;
import com.moli.iov.admin.service.VehicleTboxService;
import com.moli.iov.base.enums.DictTypeTag;
import com.moli.iov.base.enums.OnLineStatusEnum;
import com.moli.iov.base.model.dto.req.QueryVehicleTboxListParamDto;
import com.moli.iov.base.model.dto.req.VehicleTboxReqDto;
import com.moli.iov.base.model.dto.req.VehicleTboxUpdateReqDto;
import com.moli.iov.base.model.dto.res.VehicleTboxResDto;
import com.moli.iov.base.model.po.VehicleBase;
import com.moli.iov.base.model.po.VehicleTbox;
import com.moli.iov.base.remote.IDictDataRemoteService;
import com.moli.iov.base.remote.IVehicleBaseRemoteService;
import com.moli.iov.base.remote.IVehicleTboxRemoteService;
import com.moli.iov.down.model.dto.req.VehicleParamDto;
import com.moli.iov.down.model.po.VehicleParam;
import com.moli.iov.down.remote.IVehicleParamRemoteService;
import com.moli.iov.model.dto.req.PageExtend;
import com.moli.iov.real.model.res.VehicleStatusDto;
import com.moli.iov.real.service.IRealService;
import com.moli.iov.rest.RestResponse;
import com.moli.iov.util.CommonUtils;
import com.moli.iov.util.JsonUtils;
import com.moli.iov.util.StringUtil;
import com.moli.iov.util.ValidationUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;

/**
 * 处理tbox 业务层
 *
 * @author whl
 */
@Slf4j
@Service
public class VehicleTboxServiceImpl implements VehicleTboxService {

    @Reference(group = "${service.group}", check = false)
    private IVehicleTboxRemoteService iVehicleTboxRemoteService;
    @Reference(group = "${service.group}", check = false)
    private IVehicleParamRemoteService iVehicleParamRemoteService;
    @Reference(group = "${service.group}", check = false)
    private IDictDataRemoteService dictDataService;
    @Reference(group = "${service.group}", check = false)
    private IRealService iRealService;
    @Reference(group = "${service.group}", check = false)
    private IVehicleBaseRemoteService iVehicleBaseRemoteService;

    @Override
    public RestResponse<PageExtend<VehicleTboxResDto>> listVehicleTbox(QueryVehicleTboxListParamDto dto) {
        log.info("查询车载终端列表： 入参queryVehicleTboxListParamDto{}" + dto);
        return iVehicleTboxRemoteService.listVehicleTboxs(dto);
    }

    @Override
    public RestResponse updateVehicleTbox(VehicleTboxUpdateReqDto dto) {
        log.info("变更SIM卡号： 入参queryVehicleTboxListParamDto{}" + dto);
        return iVehicleTboxRemoteService.updateVehicleTbox(dto);
    }

    @Override
    public RestResponse getVehicleTboxDetails(QueryVehicleTboxListParamDto dto) throws Exception {
        ValidationUtils.validateBean(dto, QueryVehicleTboxListParamDto.Details.class);
        log.info("车辆监控-车载终端-详情： QueryVehicleTboxListParamDto:{}" + dto);
        VehicleParamDto vehicleParamDto = new VehicleParamDto();
        vehicleParamDto.setDeviceId(dto.getDeviceId());
        VehicleTbox tbox = iVehicleTboxRemoteService
                .getVehicleTboxByDeviceId(dto.getDeviceId());
        if (tbox == null) {
            return RestResponse.failure("tbox不存在");
        }
        VehicleTboxDetailVo vo = new VehicleTboxDetailVo();
        CommonUtils.copyProperties(tbox, vo);
        List<DictTypeTag> list = new ArrayList<>();
        list.add(DictTypeTag.SUPPLIERNAME);
        list.add(DictTypeTag.DEVICEMODEL);
        if(StringUtil.isNotBlank(tbox.getVin())){
            VehicleBase vehicleBaseByVin = iVehicleBaseRemoteService.getVehicleBaseByVin(tbox.getVin());
            log.info("车辆监控-车载终端-详情-base： vehicleBase:{}" + vehicleBaseByVin);
            if (vehicleBaseByVin != null) {
                vo.setLicense(vehicleBaseByVin.getLicense());
                vehicleParamDto.setVin(vehicleBaseByVin.getVin());
                RestResponse<VehicleParam> vehicleParam = iVehicleParamRemoteService
                        .getVehicleParam(vehicleParamDto);
                log.info("车辆监控-车载终端-详情-vehicleParam： vehicleParam:{}" + vehicleParam);
                if (vehicleParam != null && vehicleParam.getData() != null) {
                    CommonUtils.copyProperties(vehicleParam.getData(), vo);
                }
            }
        }
        Map<String, Map<String, String>> dictMaps = dictDataService.getDictMaps(list);
        String supplierName;
        String deviceModel;
        if (dictMaps != null && !dictMaps.isEmpty()) {
            supplierName = dictMaps.get(DictTypeTag.SUPPLIERNAME.getType()).get(tbox.getSupplierName());
            deviceModel = dictMaps.get(DictTypeTag.DEVICEMODEL.getType()).get(tbox.getDeviceModel());
            vo.setDeviceStr(supplierName + deviceModel);
        }
        List<String> vinList = new ArrayList<>();
        if (StringUtils.isNotBlank(tbox.getVin())) {
            vinList.add(tbox.getVin());
            RestResponse<Map<String, VehicleStatusDto>> vehicleStatusByVin = iRealService.getVehicleStatusByVin(vinList);
            if (vehicleStatusByVin != null && vehicleStatusByVin.getData() != null) {
                VehicleStatusDto vehicleStatusDto = vehicleStatusByVin.getData().get(tbox.getVin());
                if (vehicleStatusDto != null) {
                    if (vehicleStatusDto.getOnlineState()) {
                        vo.setOnlineStr(OnLineStatusEnum.YES.getCnName());
                    } else {
                        vo.setOnlineStr(OnLineStatusEnum.NO.getCnName());
                    }
                    if (vehicleStatusDto.getExtendLeasing() != null) {
                        vo.setRentSalePatternStr(vehicleStatusDto.getExtendLeasing().getK());
                        vo.setRentSalePattern(vehicleStatusDto.getExtendLeasing().getV());
                    }

                } else {
                    vo.setOnlineStr(OnLineStatusEnum.NO.getCnName());
                }
            }
        }
        log.info("车辆监控-车载终端-详情-res： vehicleParam:{}" + vo);
        return RestResponse.success(vo);
    }

    @Override
    public RestResponse updateVehicleTboxDeviceModel(VehicleTboxReqDto dto) {
        log.info("车辆监控-车载终端-编辑-入参vehicleParam:{}",JsonUtils.toJson(dto));
        return iVehicleTboxRemoteService.updateVehicleTbox(dto);
    }

    @Override
    public RestResponse saveVehicleTboxDeviceModel(VehicleTboxReqDto dto) {
        log.info("车辆监控-车载终端-添加-入参vehicleParam:{}",JsonUtils.toJson(dto));
        return iVehicleTboxRemoteService.saveVehicleTbox(dto);
    }


}
